from typing import Optional

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI() # app 名字自定义

class CityInfo(BaseModel):
    province: str
    country: str
    is_affected: Optional[bool] = None
    # is_affected: bool

# @app.get("/")
# def hello_world():
#     return "Hello World!"
#
# @app.get("/city/{city_name}")
# def result(city_name: str, q: Optional[str] = None):
#     return {"city_name": city_name, 'q': q}
#
# @app.put("/city/{city_name}")
# def put_result(city_name: str, city_info: CityInfo):
#     return {'city_name': city_name, 'province': city_info.province , 'country': city_info.country, 'is_affected': city_info.is_affected}


# 启动命令: uvicorn fastapi_tutorial:app --reload

# swagger: http://127.0.0.1:8000/docs
# redoc: http://127.0.0.1:8000/redoc
# http://127.0.0.1:8000/
# http://127.0.0.1:8000/city/bj?q=cy


@app.get("/")
async def hello_world():
    return "Hello World!"

@app.get("/city/{city_name}")
async def result(city_name: str, q: Optional[str] = None):
    return {"city_name": city_name, 'q': q}

@app.put("/city/{city_name}")
async def put_result(city_name: str, city_info: CityInfo):
    return {'city_name': city_name, 'province': city_info.province , 'country': city_info.country, 'is_affected': city_info.is_affected}
